﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace EncircleFTM
{
    public partial class Practice : Form
    {

        public Practice()
        {
            InitializeComponent();
        }

        public class Process
        {
            public string threadname;
            public ProgressBar pbr;
            public string ThreadNAme
            {
                get { return this.threadname; }
                set { this.threadname = value; }
            }

            public void Upload()
            {
                for (int i = 1; i < 101; i++)
                {
                    pbr.Value = i;
                    System.Threading.Thread.Sleep(1000);
                }
            }



        }

        public delegate int UDDelegate(int range, ProgressBar pbr);
        //public event UDDelegate EDEvent;
        public int UD(int value, ProgressBar pbr)
        {
            int count = 0;
            for (int i = 1; i < 100; i++)
            {
                count++;
            }
            return count;
        }

        UDDelegate objDel= null;
        private void button1_Click(object sender, EventArgs e)
        {
            //Process obj = new Process();
            //obj.pbr = progressBar1;
            //System.Threading.Thread t = new System.Threading.Thread(
            //    new System.Threading.ThreadStart(obj.Upload));
            //t.Start();

            //objDel = new UDDelegate(UD);
            //objDel.BeginInvoke(100, this.progressBar1, CallBack, null);

            
        }

        public void CallBack(IAsyncResult ar)
        {
            int returnValue = objDel.EndInvoke(ar);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int lstUpdatedVal = Convert.ToInt32(txtLastUpdatedVal.Text);
            int expiryval = Convert.ToInt32(txtExpiryVal.Text);

            if (lstUpdatedVal >= expiryval)
            {
                txtLastUpdatedVal.Text = "1";
                lstUpdatedVal = 1;
            }
            else
            {
                lstUpdatedVal++;
                txtLastUpdatedVal.Text = lstUpdatedVal.ToString();
            }
            pbr.Value = lstUpdatedVal;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
       }

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        //[DllImport("user32.dll", SetLastError = true)]
        //public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);


        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        // For Windows Mobile, replace user32.dll with coredll.dll
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        private const long WM_CLOSE = 0x10;
        private const uint BM_CLICK =(uint)0xf5L;
        private const  long WM_KEYDOWN = 0x100;
        private const  long WM_KEYUP = 0x101;
        private const  long WM_CHAR = 0x102;
        private const uint WM_SETTEXT = 0xc;


        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IntPtr WindowHandle = FindWindow(null, "Save the file as");
            IntPtr ptr =(IntPtr)0;
            IntPtr cboBoxHandle = FindWindowEx(WindowHandle,ptr, "ComboBoxEx32", "");
            IntPtr retVal = SendMessage(cboBoxHandle, WM_SETTEXT, ptr, "c:\\w.xps");
            IntPtr ButtonHandle = FindWindowEx(WindowHandle, ptr, "Button", "&Save");
            retVal = SendMessage(ButtonHandle, BM_CLICK, ptr, "0");


        }

    }
}
